package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.dataBase.UserDatabaseHelper;

public class LoginActivity extends AppCompatActivity {
    private UserDatabaseHelper User_database;
    private static int count=0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if(count == 0){
            count++;
            Intent intent = new Intent(LoginActivity.this,welcomeActivity.class);
            startActivity(intent);
        }

        EditText USERNAME;
        EditText  PASSWORD;
        Button loginButton;
        TextView registerLink;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        USERNAME = (EditText) findViewById(R.id.login_username);
        PASSWORD = (EditText)findViewById(R.id.login_password);
        loginButton = (Button) findViewById(R.id.login_button);
        registerLink = (TextView) findViewById(R.id.register_link);
        User_database = new UserDatabaseHelper(this,"User.db",null,1);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String User = USERNAME.getText().toString();
                String Password = PASSWORD.getText().toString();

                if(Utils.isValidUsername(User) && Utils.isValidPassword(Password)){
                    if(User_database.checkUser(User,Password)){
                        //User_database.insertData(User,Password);
                        Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                        //
                    }else if(User_database.checkUsername(User))
                    {
                        Toast.makeText(LoginActivity.this,"密码不正确",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(LoginActivity.this,"用户不存在",Toast.LENGTH_SHORT).show();
                    }
                }
                else{
                    Toast.makeText(LoginActivity.this,"输入不能为空",Toast.LENGTH_SHORT).show();
                }

            }
        });

        registerLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(LoginActivity.this,registerActivity.class);
                startActivity(intent);
            }
        });
    }


}
